package com.koo.modules.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koo.common.utils.MyStrUtil;
import com.koo.common.utils.PageUtils;
import com.koo.common.utils.QRCodeUtils;
import com.koo.common.utils.Query;
import com.koo.modules.store.dao.StoreInfoDao;
import com.koo.modules.store.entity.StoreInfoEntity;
import com.koo.modules.store.service.StoreInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Slf4j
@Service("storeInfoService")
public class StoreInfoServiceImpl extends ServiceImpl<StoreInfoDao, StoreInfoEntity> implements StoreInfoService {


    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        Object key = params.get("key");
        LambdaQueryWrapper<StoreInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(MyStrUtil.isNotEmpty(key), StoreInfoEntity::getStoreName, key)
                .or().eq(MyStrUtil.isNotEmpty(key), StoreInfoEntity::getStoreCode, key);

        IPage<StoreInfoEntity> page = this.page(
                new Query<StoreInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void createQRCode(String storeId, Integer storeType, HttpServletResponse response, HttpServletRequest request) {
        StoreInfoEntity store = baseMapper.selectById(storeId);
        String content = "https://member.asdzy.com/pay?storeId="+storeId;
        String logoUrl = "https://member.asdzy.com/statics/images/logo_face.png";
        if (store != null && MyStrUtil.isNotEmpty(store.getStoreLogo())) {
            logoUrl = store.getStoreLogo();
        }
        log.warn("createQRCode===basePath=={}", logoUrl);
        try {
            QRCodeUtils.encode(content, logoUrl, response.getOutputStream(), true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}